package com.wac.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.blog.pojo.Blog;
import com.wac.blog.pojo.BlogCategory;
import com.wac.blog.pojo.Comment;
import com.wac.blog.pojo.User;
import com.wac.blog.service.BlogCategoryService;
import com.wac.blog.service.BlogService;
import com.wac.blog.service.CommentService;
import com.wac.blog.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wac
 * @since 2020-08-04
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    BlogService blogService;

    @Autowired
    BlogCategoryService blogCategoryService;

    @Autowired
    CommentService commentService;

    @RequestMapping("/list")
    public String list(Model model){
        Page<Blog> page = new Page<Blog>(1, 10);
        blogService.page(page,new QueryWrapper<Blog>().orderByDesc("gmt_create").orderByDesc("views").orderByDesc("status"));
        List<Blog> blogList = page.getRecords();
        model.addAttribute("blogList",blogList );
        model.addAttribute("page",page );
        List<BlogCategory> blogCategoryList = blogCategoryService.list(null);
        model.addAttribute("blogCategoryList", blogCategoryList);
        return "blog/list";
    }

    @RequestMapping("/listView")
    public String listView(Model model){
        Page<Blog> page = new Page<Blog>(1, 10);
        blogService.page(page,new QueryWrapper<Blog>().orderByDesc("views").orderByDesc("gmt_create").orderByDesc("status"));
        List<Blog> blogList = page.getRecords();
        model.addAttribute("blogList",blogList );
        model.addAttribute("page",page );
        List<BlogCategory> blogCategoryList = blogCategoryService.list(null);
        model.addAttribute("blogCategoryList", blogCategoryList);
        return "blog/list";
    }

    @RequestMapping("/listStatus")
    public String listStatus(Model model){
        Page<Blog> page = new Page<Blog>(1, 10);
        blogService.page(page,new QueryWrapper<Blog>().orderByDesc("status").orderByDesc("views").orderByDesc("gmt_create"));
        List<Blog> blogList = page.getRecords();
        model.addAttribute("blogList",blogList );
        model.addAttribute("page",page );
        List<BlogCategory> blogCategoryList = blogCategoryService.list(null);
        model.addAttribute("blogCategoryList", blogCategoryList);
        return "blog/list";
    }

    @RequestMapping("/list/{current}/{thisCategoryName}")
    public String listByPage(Model model, @PathVariable Integer current,@PathVariable String thisCategoryName){
        if (current<1){
            current=1;
        }
        Page<Blog> page = new Page<Blog>(current, 10);
        model.addAttribute("page",page );
        if (thisCategoryName==null || thisCategoryName.equals("null")){
            blogService.page(page,new QueryWrapper<Blog>().orderByDesc("gmt_create"));
        }else {
            blogService.page(page,new QueryWrapper<Blog>().orderByDesc("gmt_create").eq("category_name", thisCategoryName));
            model.addAttribute("thisCategoryName", thisCategoryName);
        }
        List<Blog> blogList = page.getRecords();
        model.addAttribute("blogList",blogList );
        List<BlogCategory> blogCategoryList = blogCategoryService.list(null);
        model.addAttribute("blogCategoryList", blogCategoryList);
        return "blog/list";
    }

    @RequestMapping("/read/{bid}")
    public String read(Model model,@PathVariable String bid){
        Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
        model.addAttribute("blog",blog );
        blog.setViews(blog.getViews()+1);
        blogService.updateById(blog);
        List<Comment> commentList = commentService.list(new QueryWrapper<Comment>().eq("topic_id", bid));
        model.addAttribute("commentList", commentList);
        return "blog/read";
    }

    @GetMapping("/write")
    public String towrite(Model model){
        List<BlogCategory> categoryList = blogCategoryService.list(null);
        model.addAttribute("categoryList",categoryList );
        return "blog/write";
    }

    @PostMapping("/write")
    public String write(Blog blog, HttpSession session, HttpServletRequest request){
        User loginUser = (User) session.getAttribute("loginUser");
        blog.setAuthorName(loginUser.getUsername());
        blog.setAuthorId(loginUser.getUid());
        blog.setAuthorAvatar(loginUser.getAvatar());
        blog.setBid(Utils.getUuid());
        blog.setGmtCreate(Utils.getTime());
        blog.setGmtUpdate(Utils.getTime());
        blog.setStatus(0);
        blog.setViews(0);
        blog.setSort(0);
        BlogCategory blogCategory = blogCategoryService.getById(blog.getCategoryId());
        blog.setCategoryName(blogCategory.getCategory());
        blogService.save(blog);
        return "redirect:/blog/list";
    }

    @GetMapping("/editor/{uid}/{bid}")
    public synchronized String toeditor(Model model,@PathVariable String uid,@PathVariable String bid){
        Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
        model.addAttribute("blog",blog );
        if (!blog.getAuthorId().equals(uid)){
            Utils.print("禁止以非作者身份进行编辑");
        }
        List<BlogCategory> categoryList = blogCategoryService.list(null);
        model.addAttribute("categoryList",categoryList );
        return "blog/editor";
    }

    @PostMapping("/editor")
    public String editor(Blog blog){
        Blog blogSave = blogService.getOne(new QueryWrapper<Blog>().eq("bid", blog.getBid()));
        blogSave.setTitle(blog.getTitle());
        blogSave.setContent(blog.getContent());
        blogSave.setCategoryId(blog.getCategoryId());
        blogSave.setGmtUpdate(Utils.getTime());
        blogService.updateById(blogSave);
        return "redirect:/blog/read/"+blog.getBid();
    }

    //博客删除
    @RequestMapping("/delete/{uid}/{bid}")
    public String delete(@PathVariable String uid,@PathVariable String bid){
        Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
        if (!blog.getAuthorId().equals(uid)){
            Utils.print("禁止以非作者身份进行删除");
        }
        blogService.removeById(blog);
        return "redirect:/blog/list";
    }

    //评论保存
    @RequestMapping("/comment/{bid}")
    public String commentSave(@PathVariable String bid,Comment comment){
        comment.setCommentId(Utils.getUuid());
        comment.setTopicCategory(2);
        comment.setGmtCreate(Utils.getTime());
        commentService.save(comment);
        Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("bid", bid));
        blog.setStatus(1);
        blogService.updateById(blog);
        return "redirect:/blog/read/"+bid;
    }
}

